import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:woniutaofang/http/base_result.dart';
import 'package:woniutaofang/http/http_utils.dart';
import 'package:woniutaofang/model/Home/getHomeBuildings.dart';
import 'package:woniutaofang/model/login.dart';
import 'package:woniutaofang/pages/home/model/getHouseAlbumByPId.dart';
import 'package:woniutaofang/pages/home/model/getProjectInfoByPId.dart';
import 'package:woniutaofang/service/api_url.dart';
import 'package:dio/dio.dart';
import 'package:woniutaofang/utils/config.dart';

class ApiService {
  static request(String method, String url, {Map params}) async {
    Response response = await HttpUtils(
      baseUrl: Config.apiHost,
      // headers: {"Content-Type": "application/json"},
    ).request(url, method: 'GET');
    return response;
  }

  static Future<GetHomeBuildingsModel> getHomeBuildings() async {
    var response = await request('GET', ApiUrl.getHomeBuildings);
    if (response != null &&
        response.statusCode >= 200 &&
        response.statusCode < 300) {
      BaseResult result = BaseResult.fromMap(
        json.decode(response.data),
      );
      if (result.code == '0') {
        return GetHomeBuildingsModel.fromJson(json.decode(response.data));
      } else {
        return null;
      }
    } else {
      return null;
    }
  }

  static Future<GetHouseAlbumByPIdModel> getHouseAlbumByPId(String id) async {
    var response = await request('GET', ApiUrl.getHouseAlbumByPId + '/$id');
    if (response != null &&
        response.statusCode >= 200 &&
        response.statusCode < 300) {
      BaseResult result = BaseResult.fromMap(
        json.decode(response.data),
      );
      if (result.code == '0') {
        return GetHouseAlbumByPIdModel.fromJson(json.decode(response.data));
        // return response.data;
      } else {
        return null;
      }
    } else {
      return null;
    }
  }

  static Future<GetProjectInfoByPIdModel> getProjectInfoByPId(String id) async {
    var response = await request('GET', ApiUrl.getProjectInfoByPId + '/$id');
    if (response != null &&
        response.statusCode >= 200 &&
        response.statusCode < 300) {
      BaseResult result = BaseResult.fromMap(
        json.decode(response.data),
      );
      if (result.code == '0') {
        return GetProjectInfoByPIdModel.fromJson(json.decode(response.data));
      } else {
        return null;
      }
    } else {
      return null;
    }
  }
}
